import { Rank, Suit } from '@/types'
import Card from './Card'

class Deck {
  suits: Suit[]
  ranks: Rank[]
  cards: Card[]

  constructor(isRemoveJoker = false) {
    this.suits = ['♠️', '♥️', '♣️', '♦️']
    this.ranks = [
      '2',
      '3',
      '4',
      '5',
      '6',
      '7',
      '8',
      '9',
      '10',
      'J',
      'Q',
      'K',
      'A',
    ]
    this.cards = []
    this.initializeDeck(isRemoveJoker)
  }

  initializeDeck(isRemoveJoker: boolean) {
    this.cards = []
    for (const suit of this.suits) {
      for (const rank of this.ranks) {
        this.cards.push(new Card(suit, rank))
      }
    }
    if (!isRemoveJoker) {
      // 加入大小王（斗地主需要）
      this.cards.push(new Card('🃏', 'Joker Big'))
      this.cards.push(new Card('🃏', 'Joker Small'))
    }
  }

  // 洗牌
  shuffle() {
    for (let i = this.cards.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1))
      ;[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]]
    }
  }

  // 发牌
  deal(num: number) {
    return this.cards.splice(0, num)
  }
}

export default Deck
